<?php
/**
 * @Author: liuhongbin
 * @Email: 514660094@qq.com
 * @Date: 2019-02-12 16:15:10
 * @Description:
 * @LastEditors: liuhongbin
 * @LastEditTime: xxx
 */

namespace addons\cloud_market\controller\api\developer;

use addons\cloud_market\controller\api\CloudMarketApiController;
use hiduoke\services\FormBuilder as Form;

class Setting extends CloudMarketApiController
{
    protected $notApiLogin = [];
    protected $notApiCheck = [];

    protected $dev_id;
    protected $user_id;

    protected function initialize()
    {
        parent::initialize(); // TODO: Change the autogenerated stub
        $user = $this->getUser();
        if (!$user['developer']) {
            showMessage("您没有权限", 'error', '', '-1');
        }
        $this->dev_id = $user['developer']['id'];
        $this->user_id = $user['id'];
    }


    public function getSettingForm()
    {
        $row = $this->getUser()['developer'];
        $action = $this->request->domain() . "/api.developer.setting/saveSetting";
        $form = Form::createForm($action)->setMethod("POST");
        //新增表单
        $rule = [
            Form::input('name', '店铺名称', $row['name'] ?? '')->required(),
            Form::textarea('description', '简介', $row['description'] ?? ''),
            Form::dkImage('logo', 'LOGO', $row['logo'] ?? '')->required(),
            Form::dkGroup("kefu", '客服列表', $row['kefu'] ?? [])
                ->rule([
                    Form::input('name', '客服名', '')->required(),
//                    Form::radio('work', "工作", '售前')->options([
//                        ['label' => '售前', 'value' => '售前'],
//                        ['label' => '售后', 'value' => '售后'],
//                        ['label' => '开发', 'value' => '开发'],
//                        ['label' => '闲聊', 'value' => '闲聊'],
//                    ]),
                    Form::input('work', '负责内容', '')->effect(['help'=>'设置工作内容，如：开发、售后、售前、闲聊等']),
                    Form::dkImage('qr', '二维码', '')->effect(['help' => '填写客服的联系二维码'])
                ]),
        ];
        $title = "编辑站点";
        $form->setTitle($title)->setRule($rule);
        return $this->success("", Form::getFormData($form));
    }

    public function getAuthSettingForm()
    {
        $row = $this->getUser()['developer'];
        $action = $this->request->domain() . "/api.developer.setting/saveSetting";
        $form = Form::createForm($action)->setMethod("POST");
        //新增表单
        $rule = [
            Form::radio('type', '类型', $row['type'] ?? '1')
                ->options([
                    ['label' => '企业', 'value' => '1'],
                    ['label' => '个人', 'value' => '0']
                ])->appendControl('1', [
                    Form::input('truename', '姓名/企业名', $row['truename'] ?? '')->required(),
                    Form::input('company_code', '统一社会信用代码', $row['company_code'] ?? ''),
                    Form::dkImage('business_license_image', '营业执照', $row['business_license_image'] ?? '')->required(),
                ])->appendControl('0', [
                    Form::input('truename', '姓名', $row['truename'] ?? '')->required(),
                    Form::switches('is_student', '是否为学生', $row['is_student'] ?? 0),
                ]),
            Form::input('phone', '联系电话', $row['phone'] ?? '')->required(),
            Form::radio('experience', '开发经验', $row['experience'] ?? '1')
                ->options([
                    ['label' => '1年以下', 'value' => '1'],
                    ['label' => '1-3年', 'value' => '2'],
                    ['label' => '3-5年', 'value' => '3'],
                    ['label' => '5年以上', 'value' => '4']
                ]),
        ];
        $title = "编辑站点";
        $form->setTitle($title)->setRule($rule);
        return $this->success("", Form::getFormData($form));
    }

    public function getNewToken()
    {
        $token = md5(uniqid());
        $this->getUser()['developer']->token = $token;
        $this->getUser()['developer']->save();
        return $this->success("保存成功", $token);
    }

    public function saveSetting()
    {
        $developer = $this->getUser()['developer'];
        if($developer['status'] == 0){
            return $this->error('你的申请正在审核中');
        }
        $formData = getFormData();
        $developer->save($formData);
        return $this->success("保存成功", $formData);
    }

}
